Imports System.Data.SqlClient
Imports NENAData.Errors
Imports NENAData.NENAMember

Public Class ViewProvincalMembers
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents dtgNewMembers As System.Web.UI.WebControls.DataGrid
    Protected WithEvents txtAffiliateID As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtMemberID As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtAffiliate As System.Web.UI.WebControls.TextBox
    Protected WithEvents lblTitle As System.Web.UI.WebControls.Label
    Protected WithEvents DG_AffiliateSummary As System.Web.UI.WebControls.DataGrid

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strAffiliateID As String

        strAffiliateID = Request.QueryString("AffiliateID")
        'verify user is accessing an affiliate they have rights to
        'VerifyAffiliate(strAffiliateID)

        If Not IsPostBack Then
            'load only data from the specified affiliate
            BindDataList(strAffiliateID)
            ShowAffiliateMembershipDetails(strAffiliateID)
            txtAffiliateID.Text = strAffiliateID
            lblTitle.Text &= txtAffiliate.Text
        End If
    End Sub
    Private Sub ShowAffiliateMembershipDetails(ByVal strAffiliateID As String)

        Dim conPubs As SqlConnection
        Dim cmdSelect As SqlCommand
        Dim dtrTitles As SqlDataReader
        Dim strSql As String

        strSql = "select StatusDescription, count(1) TCount from Members, Status " & _
            "where  " & _
            "affiliate_id =  '" & strAffiliateID & "' " & _
            "and Account_Status = status_id " & _
            "group by StatusDescription;"

        conPubs = New SqlConnection(ConfigurationManager.AppSettings.Item("SQLConnectionString"))
        cmdSelect = New SqlCommand(strSql, conPubs)
        conPubs.Open()
        dtrTitles = cmdSelect.ExecuteReader()

        DG_AffiliateSummary.DataSource = dtrTitles
        DG_AffiliateSummary.DataBind()

        dtrTitles.Close()
        conPubs.Close()
    End Sub
    Sub BindDataList(ByVal strAffiliateID As String)
        Dim conPubs As SqlConnection
        Dim cmdSelect As SqlCommand
        Dim dtrTitles As SqlDataReader
        Dim strSql As String

        strSql = "select distinct * from members, affiliate where " & _
       "(Account_Status = '" & ConfigurationManager.AppSettings.Item("MemberStatusPending") & "' or " & _
       "Account_Status = '" & ConfigurationManager.AppSettings.Item("MemberStatusRenewing") & "') " & _
       " and members.affiliate_id = affiliate.affiliate_id " & _
       " and members.affiliate_id = '" & strAffiliateID & "' " & _
       " order by lastName"

        conPubs = New SqlConnection(ConfigurationManager.AppSettings.Item("SQLConnectionString"))
        cmdSelect = New SqlCommand(strSql, conPubs)
        conPubs.Open()
        dtrTitles = cmdSelect.ExecuteReader()

        dtgNewMembers.DataSource = dtrTitles
        dtgNewMembers.DataBind()

        dtrTitles.Close()
        conPubs.Close()
    End Sub
    Public Sub dtgNewMembers_Cancel(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles dtgNewMembers.CancelCommand
        dtgNewMembers.EditItemIndex = -1
        'remove the edititemindex
        BindDataList(txtAffiliateID.Text)
        'refresh the grid
    End Sub
    Private Sub dtgNewMembers_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dtgNewMembers.EditCommand
        dtgNewMembers.EditItemIndex = e.Item.ItemIndex
        'set details index
        'remove the counselors editindex 
        BindDataList(txtAffiliateID.Text)
        'BindTheGrid()
        'refresh grids
    End Sub
    Private Sub dtgNewMembers_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dtgNewMembers.ItemCommand
        Dim oRoles As List(Of TelligentCommunity.Roles) = Session("Roles")

        If oRoles.Contains(TelligentCommunity.Roles.Membership_NoSave) Then
            Response.Redirect("default.aspx")
        End If

        txtMemberID.Text = CType(e.Item.FindControl("lblMemberID"), Label).Text
        'need to change the ID variables

        Select Case e.CommandName
            Case "View"

                dtgNewMembers.EditItemIndex = -1
                'remove the edit index
                BindDataList(txtAffiliateID.Text)
                'refresh counselor display

                'dtgDetails.EditItemIndex = -1
                'BindDetails()
                'refresh details display

        End Select

        dtgNewMembers.SelectedIndex = e.Item.ItemIndex
        'set the selectedindex to apply 
        'selecteditem formatting

    End Sub
    Public Sub dtgNewMembers_Update(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles dtgNewMembers.UpdateCommand

        Dim intAffiliateID As Integer = CType(txtAffiliateID.Text, Integer)
        Dim intMemberID As Integer = CType(txtMemberID.Text, Integer)

        Try

            Dim oActivate As Account.Activate = New Account.Activate
            oActivate.MembershipAccount(intAffiliateID, intMemberID, True, NENAData.Payments.NENAPayments.PaidBy.Cheque, 0)

            'finally, refresh the screen
            dtgNewMembers.EditItemIndex = -1
            BindDataList(txtAffiliateID.Text)

        Catch ex As Exception
            ErrHandler.WriteError("Failed dtgNewMembers_Update; Marking Member Paid: " + intMemberID.ToString + "\r\n Error: " + ex.Message)
        End Try
    End Sub
#Region "Create Paid Array"
    Protected Function Create_Paid_Array() As DataTable
        Dim dt As New DataTable
        Dim dr As DataRow

        dt.Columns.Add(New DataColumn("ID", GetType(String)))
        dt.Columns.Add(New DataColumn("Description", GetType(String)))

        dr = dt.NewRow()
        dr(0) = "N"
        dr(1) = "Not Paid"
        dt.Rows.Add(dr)
        dr = dt.NewRow()
        dr(0) = "Y"
        dr(1) = "Paid"
        dt.Rows.Add(dr)

        Return dt
    End Function
#End Region

#Region " Verifying Affiliate ID "

    Public Sub VerifyAffiliate(ByVal strAffiliateID As String)

        Dim oRoles As List(Of TelligentCommunity.Roles) = Session("Roles")

        Select Case strAffiliateID
            Case 1
                If oRoles.Contains(TelligentCommunity.Roles.MembershipAdministrator) Or _
                        oRoles.Contains(TelligentCommunity.Roles.ENABC_Membership) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleBC")
                Else
                    Response.Redirect("")
                End If
            Case 2
                If oRoles.Contains(TelligentCommunity.Roles.MembershipAdministrator) Or _
                        oRoles.Contains(TelligentCommunity.Roles.ENIG_Membership) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleAB")
                Else
                    Response.Redirect("")
                End If
            Case 3
                If oRoles.Contains(TelligentCommunity.Roles.MembershipAdministrator) Or _
                        oRoles.Contains(TelligentCommunity.Roles.SENG_Membership) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleSK")
                Else
                    Response.Redirect("")
                End If
            Case 4
                If oRoles.Contains(TelligentCommunity.Roles.MembershipAdministrator) Or _
                        oRoles.Contains(TelligentCommunity.Roles.EDNA_Membership) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleMB")
                Else
                    Response.Redirect("")
                End If
            Case 5
                If oRoles.Contains(TelligentCommunity.Roles.MembershipAdministrator) Or _
                        oRoles.Contains(TelligentCommunity.Roles.ENAO_Membership) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleONT")
                Else
                    Response.Redirect("")
                End If
            Case 6
                If oRoles.Contains(TelligentCommunity.Roles.MembershipAdministrator) Or _
                        oRoles.Contains(TelligentCommunity.Roles.NBENA_Membership) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleNB")
                Else
                    Response.Redirect("")
                End If
            Case 7
                If oRoles.Contains(TelligentCommunity.Roles.MembershipAdministrator) Or _
                        oRoles.Contains(TelligentCommunity.Roles.NSENA_Membership) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleNS")
                Else
                    Response.Redirect("")
                End If
            Case 8
                If oRoles.Contains(TelligentCommunity.Roles.MembershipAdministrator) Or _
                        oRoles.Contains(TelligentCommunity.Roles.PEIENA_Membership) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RolePEI")
                Else
                    Response.Redirect("")
                End If
            Case 9
                If oRoles.Contains(TelligentCommunity.Roles.MembershipAdministrator) Or _
                        oRoles.Contains(TelligentCommunity.Roles.NLENA_Membership) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleNF")
                Else
                    Response.Redirect("")
                End If
            Case 10
                If oRoles.Contains(TelligentCommunity.Roles.MembershipAdministrator) Or _
                        oRoles.Contains(TelligentCommunity.Roles.International_Membership) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleOTHER")
                Else
                    Response.Redirect("")
                End If
            Case 11
                If oRoles.Contains(TelligentCommunity.Roles.MembershipAdministrator) Or _
                        oRoles.Contains(TelligentCommunity.Roles.Independant_Membership) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleOTHER")
                Else
                    Response.Redirect("")
                End If
        End Select
    End Sub
#End Region

End Class
